package com.qianfeng.smartplatform.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.smartplatform.cache.SceneCache;
import com.qianfeng.smartplatform.constants.ResultEnum;
import com.qianfeng.smartplatform.exceptions.AddErrorException;
import com.qianfeng.smartplatform.exceptions.DeleteErrorException;
import com.qianfeng.smartplatform.exceptions.QueryDataException;
import com.qianfeng.smartplatform.exceptions.UpdateErrorException;
import com.qianfeng.smartplatform.mapper.SceneMapper;
import com.qianfeng.smartplatform.pojo.CheckType;
import com.qianfeng.smartplatform.pojo.Scene;
import com.qianfeng.smartplatform.pojo.SceneExample;
import com.qianfeng.smartplatform.security.BaseUser;
import com.qianfeng.smartplatform.service.SceneService;
import com.qianfeng.smartplatform.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Jackiechan on 2021/12/26/13:00
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class SceneServiceImpl implements SceneService {
    private SceneMapper sceneMapper;

    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    @Autowired
    public void setSceneMapper(SceneMapper sceneMapper) {
        this.sceneMapper = sceneMapper;
    }

    @Override
    public void AddScene(Scene scene) {
        Assert.isTrue(scene!=null&&!scene.isEmpty(CheckType.ADD), ()->{
            throw new AddErrorException(ResultEnum.NULL_DATA);
        });
        BaseUser baseUser = SecurityUtils.getUser();
        scene.setCreateBy(baseUser.getId());//设置创建者的id
        scene.setCreateTime(new Date());
        sceneMapper.insertSelective(scene);
    }

    @Override
    public void updateScene(Scene scene) {
        Assert.isTrue(scene!=null&&!scene.isEmpty(CheckType.UPDATE), ()->{
            throw new AddErrorException(ResultEnum.NULL_DATA);
        });
        //更新数据
        int result = sceneMapper.updateByPrimaryKeySelective(scene);
        Assert.isTrue(result>0, ()->{
            throw new UpdateErrorException(ResultEnum.DATA_NOT_EXIST);//数据不存在
        });
    }

    @Override
    public Scene getSceneById(Long id) {
        Assert.isTrue(!ObjectUtils.isEmpty(id)&&id>0, ()->{
            throw new QueryDataException(ResultEnum.ID_NULL);
        });
        Scene scene = sceneMapper.selectByPrimaryKey(id);
        //根据业务需要决定如果数据是空的如何处理,比如空的,返回null或者返回提示数据
        Assert.notNull(scene,()->{
            throw new QueryDataException(ResultEnum.DATA_NOT_EXIST);
        });

        return scene;
    }

    @Override
    public List<Scene> getAllScenesByUser() {
        BaseUser baseUser = SecurityUtils.getUser();
        Integer userId = baseUser.getId();//获取当前用户的id
        List<Scene> sceneList = sceneCache.getUserScene().getIfPresent(userId);
        return sceneList;
    }

    @Override
    public void deleteScenes(List<Long> ids) {
        Assert.notEmpty(ids,()->{
            throw new DeleteErrorException(ResultEnum.NULL_DATA);
        });
        BaseUser user = SecurityUtils.getUser();
        SceneExample sceneExample = new SceneExample();
        sceneExample.createCriteria()
                .andSceneIdIn(ids)
                .andCreateByEqualTo(user.getId());//这个地方要注意,可能会删除其他人的场景,所以应该要判断场景是不是当前用户判断的

        sceneMapper.deleteByExample(sceneExample);
    }
}
